import tinymce from 'tinymce'
import { v4 as uuid } from 'uuid'
import { parseFullContent, inTermBlock } from '@/utils/helpers'
import { isListTitle } from '@/constants/structure'

export class StdParagraphV2 {
  constructor(node) {
    this.node = node
    // 剔除掉空行和nodeType=8的注释
    this.childNodes = [...node.childNodes].filter((node) => {
      return node.nodeName !== 'BR' && node.nodeType !== 8
    })
  }

  get data() {
    // 索引页内容，单独处理
    if (this.node?.dataset?.referredId) {
      const { content, contentNumber } = this.node?.dataset || {}
      return {
        dataId: this.node?.dataset?.id,
        dataLang: this.node?.dataset?.lang,
        contentList: [{ content, contentNumber, fullContent: content }]
      }
    }
    if (isListTitle(this.node?.dataset?.type) && !inTermBlock(this.node)) {
      const fullContent = this.node?.innerText
      const { content, contentEn } = parseFullContent(fullContent)
      return {
        dataId: this.node?.dataset?.id,
        dataLang: this.node?.dataset?.lang,
        contentList: [{ contentType: 'text', content, contentEn, fullContent }]
      }
    }
    const referenceNode = tinymce.activeEditor.dom.getParent(this.node, '.page-sheet.reference')
    if (referenceNode && this.node?.dataset?.type === 'paragraph') {
      const paraNodes = [...referenceNode.querySelectorAll('p[data-type="paragraph"]')]
      const index = paraNodes.findIndex((node) => node === this.node)
      return {
        dataId: this.node?.dataset?.id,
        contentList: this.childNodes.map((childNode) => new ReferenceParaTextNode(childNode, index + 1).data)
      }
    }
    const data = {
      dataId: this.node?.dataset?.id,
      dataLang: this.node?.dataset?.lang,
      isExample: Number(this.node.dataset?.isExample) ? true : false
    }

    if (this.node?.classList?.contains('nr-para')) {
      data.contentList = this.getNRContent(this.node)
    } else {
      data.contentList = this.childNodes.map(this.getContent)
    }

    const paraAttrs = this.node?.dataset

    if (paraAttrs?.refcontent) {
      data.contentList[0].bizExtra = {
        sourceId: paraAttrs?.sourceId || '',
        refcontent: paraAttrs?.refcontent || '',
        refversion: paraAttrs?.refversion || '',
        mainContentId: paraAttrs?.maincontentid || '',
        refname: paraAttrs?.refname || '',
        refdate: paraAttrs?.refdate || '',
        docno: paraAttrs?.docno || '',
        linkUrl: paraAttrs?.linkUrl || ''
      }
    }
    return data
  }

  getNRContent(node) {
    const childNodes = [...node.childNodes]
    const nrCodeNode = childNodes.find((i) => i?.classList?.contains('nr-code'))
    const nrNameNode = childNodes.find((i) => i?.classList?.contains('nr-name'))
    const content = `${nrCodeNode?.innerText || ''}   ${nrNameNode?.innerText || ''}`

    return [
      {
        contentType: 'text',
        linkUrl: node?.dataset?.linkUrl || '',
        content: content,
        fullContent: content
      }
    ]
  }

  /** paragraph节点内容，作为行内元素 */
  getContent(node) {
    if (node.nodeType === 3) {
      return new ParaTextNode(node).data // 纯文本节点
    }

    if (node?.dataset?.type === 'footnote' || node?.dataset?.type === 'table_footnote') {
      return new ParaFootnoteNode(node).data // 脚注
    }

    return new ParaChildNode(node).data //sup, sub, department通过data-block-type识别span
  }
}

class ParaTextNode {
  constructor(node) {
    this.node = node
  }
  get data() {
    return {
      contentType: 'text',
      linkUrl: this.node.parentNode?.dataset?.linkUrl || '',
      content: this.node.nodeValue,
      fullContent: this.node.nodeValue
    }
  }
}

class ReferenceParaTextNode {
  constructor(node, contentNumber) {
    this.node = node
    this.contentNumber = contentNumber
  }

  get data() {
    return {
      contentType: 'text',
      content: this.node.nodeValue,
      contentNumber: this.contentNumber,
      fullContent: `[${this.contentNumber}]  ${this.node.nodeValue}`
    }
  }
}

class ParaFootnoteNode {
  constructor(node) {
    this.node = node
  }
  get data() {
    const contentType = this.node?.dataset?.type
    if (contentType === 'table_footnote') {
      // 表脚注
      return {
        contentType,
        contentNumber: this.node?.dataset?.number,
        content: this.node?.textContent,
        dataId: this.node?.dataset?.id || uuid(),
        fullContent: this.node?.dataset?.number + ' ' + this.node?.textContent
      }
    }
    // 条文脚注
    const contentZh = this.node?.dataset?.contentZh || ''
    const contentEn = this.node?.dataset?.contentEn || ''
    const number = this.node?.dataset?.number
    return {
      contentType,
      contentNumber: number,
      content: contentZh,
      dataId: this.node?.dataset?.id || uuid(),
      fullContent: `${number}) ${contentZh}`,
      contentEn: contentEn,
      fullContentEn: contentEn ? `${number}) ${contentEn}` : ''
    }
  }
}
class ParaChildNode {
  constructor(node) {
    this.node = node
  }
  get data() {
    const contentType = this.node?.dataset?.type
    const content = this.node?.firstChild?.nodeValue
    const contentData = { contentType, content, fullContent: content }
    if (contentType === 'inline_formula') {
      contentData.dataId = this.node?.dataset?.id
      contentData.formulaData = {
        url: this.node?.src,
        width: this.node?.width,
        height: this.node?.height,
        latex: this.node?.dataset?.latex || ''
      }
    }
    if (contentType === 'inline_figure') {
      contentData.dataId = this.node?.dataset?.id
      contentData.figureData = {
        dataId: this.node?.dataset?.id,
        attributes: {
          url: this.node?.src,
          width: this.node?.width,
          height: this.node?.height
        }
      }
    }
    if (contentType === 'keyword') {
      contentData.dataId = this.node?.dataset?.id
      contentData.content = this.node?.dataset?.content
    }
    if (contentType === 'bizIndex') {
      contentData.dataId = this.node?.dataset?.id
      contentData.content = this.node?.dataset?.content
      contentData.contentNumber = this.node?.dataset?.contentNumber
    }
    if (contentType === 'refdoc' || contentType === 'refchapter') {
      contentData.content = this.node?.dataset?.refcontent
      contentData.fullContent = this.node?.innerText
      contentData.dataId = this.node?.dataset?.id
      contentData.linkUrl = this.node?.dataset?.linkUrl || ''
      contentData.bizExtra = JSON.stringify({
        refname: this.node?.dataset?.refname || '',
        standardId: this.node?.dataset?.standardid || '',
        mainContentId: this.node?.dataset?.maincontentid || '',
        refclassification: this.node?.dataset?.refclassification || '',
        refdate: this.node?.dataset?.refdate || '',
        refcode: this.node?.dataset?.refcode || this.node?.dataset?.refcontent || '',
        refanchor: this.node?.dataset?.refanchor || '',
        docno: this.node?.dataset?.docno || '',
        refversion: this.node?.dataset?.refversion || '',
        category: this.node?.dataset?.category || ''
      })
    }
    if (contentType?.includes('variable')) {
      contentData.bizExtra = JSON.stringify({
        variableId: this.node?.dataset?.variableId || '',
      })
    }
    return contentData
  }
}
